package com.hou.controller;

import com.hou.parma.PageParam;
import com.hou.pojo.UserBasic;
import com.hou.pojo.Vip;
import com.hou.service.IUserBasicService;
import com.hou.vo.CaseVip;
import com.hzk.vo.RespBean;
import com.hzk.vo.RespBeanEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @auoter:jackh
 */
@RestController
@Controller
@Slf4j
@RequestMapping("/hou")
public class userController {
    @Autowired
    IUserBasicService iUserBasicService;
    @Autowired
    RedisTemplate redisTemplate;

    @RequestMapping("/user/select")
    public RespBean user(@CaseVip Vip vip, PageParam pageParam) {
        if (vip == null) {
            return RespBean.error(RespBeanEnum.NULL_USER_ERROR);
        }
        Long i = iUserBasicService.selectCount();
        List list = iUserBasicService.selectLists(pageParam);
        if (list == null) {
            return RespBean.error(RespBeanEnum.NULL_USER_LIST);
        }
        return new RespBean(200, "SUCCESS", i, list);
    }

    @RequestMapping("/user/add")
    public RespBean add(@CaseVip Vip vip, UserBasic userBasic) {
        if (vip == null) {
            return RespBean.error(RespBeanEnum.NULL_USER_ERROR);
        }
        int i;
        try {
            i = iUserBasicService.save1(userBasic);
        } catch (Exception exception) {
            log.info(exception.getMessage());
            return RespBean.error(RespBeanEnum.OPRATION_ERROR);
        }
        if (i == 0) {
            return RespBean.error(RespBeanEnum.OPRATION_ERROR);
        }
        return RespBean.success();
    }

    @RequestMapping("/user/update")//,
    public RespBean update(@CaseVip Vip vip,UserBasic userBasic,HttpServletRequest request) {
        /*localStorage.getItem("token")*/

        if (vip==null){
            return RespBean.error(RespBeanEnum.NULL_USER_ERROR);

        }
        /*String token = request.getHeader("token");
        if (token == null) {
            return RespBean.error(RespBeanEnum.OPRATION_ERROR);
        }
//        UserBasic user = iUserBasicService.getUserByCookies(s, Response, Request);
        Vip vip = (Vip) redisTemplate.opsForValue().get("VIP:" + token);
        if (vip == null) {
            return RespBean.error(RespBeanEnum.NULL_USER_ERROR);
        }*/
        if (userBasic == null) {
            return RespBean.error(RespBeanEnum.OPRATION_ERROR);
        }
        boolean save = iUserBasicService.updateById(userBasic);
        if (!save) {
            return RespBean.error(RespBeanEnum.OPRATION_ERROR);
        }
        return RespBean.success();
    }

    @RequestMapping("/user/delete")
    public RespBean delete(@CaseVip Vip vip,Long id) {
        boolean save = iUserBasicService.removeById(id);
        if (!save) {
            return RespBean.error(RespBeanEnum.OPRATION_ERROR);
        }
        return RespBean.success();
    }

    @RequestMapping("/user/search")
    public RespBean search(@CaseVip Vip vip,PageParam pageParam) {
        if (vip == null) {
            return RespBean.error(RespBeanEnum.NULL_USER_ERROR);
        }
        List<UserBasic> search = iUserBasicService.search(pageParam);
        Long i = iUserBasicService.selectCount2(pageParam.getSearch());
        if (search == null) {
            return RespBean.error(RespBeanEnum.OPRATION_ERROR);
        }
        return new RespBean(200, "SUCCESS", i, search);
    }

    @RequestMapping("/user/searchid")
    public RespBean searchid(@CaseVip Vip vip,Long id) {
        if (vip == null) {
            return RespBean.error(RespBeanEnum.NULL_USER_ERROR);
        }
        UserBasic userBasic = iUserBasicService.selectid(id);
        if (userBasic == null) {
            return RespBean.error(RespBeanEnum.OPRATION_ERROR);
        }
        return RespBean.success(userBasic);
    }

}
